package com.thx.user.listener;

import java.util.Hashtable;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.thx.common.web.WebConstant;
import com.thx.user.model.User;


public class UserSessionListener implements HttpSessionListener,
		HttpSessionAttributeListener {

    /**
     * web端session集合
     */
	public  static Map<String,HttpSession> USER_SESSION_MAP = new Hashtable<String,HttpSession>();
	/**
	 * 手机客户端session集合
	 */
	public  static Map<String,HttpSession> USER_SESSION_RPC_MAP = new Hashtable<String,HttpSession>();

	public void attributeAdded(HttpSessionBindingEvent event) {
		if (event.getName().equals(WebConstant.KEY_USER)) {
			HttpSession session = event.getSession();
			User user = (User) event.getValue();
			synchronized (USER_SESSION_MAP) {
				if(USER_SESSION_MAP.containsKey(user.getUserId()) ){
					HttpSession oldSession =USER_SESSION_MAP.get(user.getUserId());
					USER_SESSION_MAP.remove(user.getUserId());
					oldSession.invalidate();
				}
				USER_SESSION_MAP.put(user.getUserId(), session);
			}

		}
		if (event.getName().equals(WebConstant.KEY_RPC_USER)) {
          HttpSession session = event.getSession();
          User user = (User) event.getValue();
          synchronized (USER_SESSION_RPC_MAP) {
              if(USER_SESSION_RPC_MAP.containsKey(user.getUserId()) ){
                  HttpSession oldSession =USER_SESSION_RPC_MAP.get(user.getUserId());
                  USER_SESSION_RPC_MAP.remove(user.getUserId());
                  oldSession.invalidate();
              }
              USER_SESSION_RPC_MAP.put(user.getUserId(), session);
          }
        }
	}

	public void attributeRemoved(HttpSessionBindingEvent event) {
		if (event.getName().equals(WebConstant.KEY_USER)) {
			User user = (User) event.getValue();
			synchronized (USER_SESSION_MAP) {
				USER_SESSION_MAP.remove(user.getUserId());
			}
		}
		if (event.getName().equals(WebConstant.KEY_RPC_USER)) {
          User user = (User) event.getValue();
          synchronized (USER_SESSION_RPC_MAP) {
            USER_SESSION_RPC_MAP.remove(user.getUserId());
          }
        }
	}

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sessionDestroyed(HttpSessionEvent event) {
		HttpSession session = event.getSession();
		Object user = session.getAttribute(WebConstant.KEY_USER);
		Object userRpc = session.getAttribute(WebConstant.KEY_RPC_USER);
		if(user!=null){
			if(USER_SESSION_MAP.containsKey(((User) user).getUserId())){
				USER_SESSION_MAP.remove(((User) user).getUserId());
			}
		}
		if(userRpc!=null){
          if(USER_SESSION_MAP.containsKey(((User) userRpc).getUserId())){
              USER_SESSION_MAP.remove(((User) userRpc).getUserId());
          }
        }
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent se) {
		// TODO Auto-generated method stub

	}

}
